package cn.edu.zjicm.wordsnet_d.activity;

import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.adapter.RechargeAdapter;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.pay.Alipay;
import cn.edu.zjicm.wordsnet_d.pay.Product;
import cn.edu.zjicm.wordsnet_d.synchronize.PayInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;

public class RechargeActivity extends WordBaseActivity implements OnClickListener {

//	String[] beanStr = { "500ZM豆=5元", "1500ZM豆=13元", "3000ZM豆=24元", "6000ZM豆=42元" };

	private ListView selectBeanLv;
	private Button rechargeBtn;
	private TextView showAgreement;
	private int selectIndex = -1;
	private List<Product> productList;;
	private RechargeAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("支付宝充值");

		setContentView(R.layout.activity_recharge);

		selectBeanLv = (ListView) findViewById(R.id.recharge_lv);
		rechargeBtn = (Button) findViewById(R.id.rechargeBtn);
		showAgreement = (TextView) findViewById(R.id.show_agreement);

		productList = WordFactory.getInstance(this).getProducts(0);
		adapter = new RechargeAdapter(productList, this);
		selectBeanLv.setAdapter(adapter);

		selectBeanLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				adapter.setSelection(position);
				adapter.notifyDataSetChanged();
				selectIndex = position;
			}
		});

		rechargeBtn.setOnClickListener(this);
		showAgreement.setOnClickListener(this);
		
		getProductList();
//		PayInterface.getInstance(this).getUserWealth();
//		PayInterface.getInstance(this).getUserBought();
//		PayInterface.getInstance(this).buyWithZmd("18");
//		PayInterface.getInstance(this).buyWithZmd("19");
//		PayInterface.getInstance(this).buyWithZmd("20");
//		doCreateAlipayOrder("1");
//		doCreateAlipayOrder("18");		
//		doCreateAlipayOrder("10001");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == rechargeBtn) {
			if (selectIndex == -1)
				Toast.makeText(this, "请先选择充值金额", Toast.LENGTH_SHORT).show();
			else if (selectIndex >= 0) {
				doCreateAlipayOrder(productList.get(selectIndex).getId() + "");
			}
		}else if(v==showAgreement){
			AgreementActivity.startActivity(this, AgreementActivity.TYPE_RECHARGE);
		}
	}
	
	void doCreateAlipayOrder(String productID){
		createAlipayOrderHandler.sendMessage(createAlipayOrderHandler.obtainMessage(UserInterface.MSG_START));
		PayInterface.getInstance(this).createAlipayOrder(productID, createAlipayOrderHandler);
	}
	
	Handler createAlipayOrderHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(RechargeActivity.this, null, "正在连接服务器");
				break;
			case UserInterface.MSG_SUCCESS:
				String orderID = (String) msg.obj;
				Product product = productList.get(selectIndex);
				String subject = product.getProductName() + "="	+ product.getPrice() + "元";
				double price = product.getPrice();
//				double price = (selectIndex + 1) / 100.0;
//				double price = product.getPrice() / 100.0;
				new Alipay(RechargeActivity.this).pay(orderID, product.getProductName(), subject, price + "");
				break;
			case UserInterface.MSG_UNSUCCESS:
				Toast.makeText(RechargeActivity.this, "连接服务器失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ERROR:
				Toast.makeText(RechargeActivity.this, "连接服务器失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;			
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(RechargeActivity.this, "请确认网络是否连接", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
				Toast.makeText(RechargeActivity.this, "网络未连接，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};
	
	void getProductList(){
//		getProductListHandler.sendMessage(getProductListHandler.obtainMessage(UserInterface.MSG_START));
		PayInterface.getInstance(this).getCommodityList(getProductListHandler);
	}
	
	Handler getProductListHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(RechargeActivity.this, null, "正在获取商品信息");
				break;
			case UserInterface.MSG_SUCCESS:
				productList = WordFactory.getInstance(RechargeActivity.this).getProducts(0);
				adapter = new RechargeAdapter(productList, RechargeActivity.this);
				selectBeanLv.setAdapter(adapter);
				break;
			case UserInterface.MSG_UNSUCCESS:
//				Toast.makeText(RechargeActivity.this, "获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ERROR:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};
	

}
